package Application.Widgets.TextFileds;

import Application.Widgets.ErrorReportWidget;

import javax.swing.*;

public class TextFiledDouble extends JTextField
{
    private String WidgetName = "";
    private double BaseValue;

    public TextFiledDouble(double baseValue)
    {
        this.BaseValue = baseValue;
    }

    public void setWidgetName(String name)
    {
        this.WidgetName = name;
    }
    public String getWidgetName() {return this.WidgetName;}

    public double getValue()
    {
        double value = BaseValue;
        try {
            value = Double.parseDouble(this.getText());
        }catch (Exception exception)
        {
            exception.printStackTrace();
            ErrorReportWidget.errorReport.append(exception.getMessage()+"\n错误的参数，位置："+this.getWidgetName()+"\n");
        }
        return value;
    }
    public double getValue(double minNum,double maxNum)
    {
        double value = BaseValue;
        try {
            value = Double.parseDouble(this.getText());
        }catch (Exception exception)
        {
            exception.printStackTrace();
            ErrorReportWidget.errorReport.append(exception.getMessage()+"\n错误的参数，位置："+this.getWidgetName()+"\n");
        }
        if (value > maxNum)
        {
            value = maxNum;
            ErrorReportWidget.errorReport.append("系统：超出最大值，已自动设置为理论最大值,位置："+this.getWidgetName()+"\n");
        }else if (value < minNum)
        {
            value = minNum;
            ErrorReportWidget.errorReport.append("系统：超出最小值，已自动设置为理论最小值,位置："+this.getWidgetName()+"\n");
        }
        return value;
    }
    public double getValue(double minNum)
    {
        double value = BaseValue;
        try {
            value = Double.parseDouble(this.getText());
        }catch (Exception exception)
        {
            exception.printStackTrace();
            ErrorReportWidget.errorReport.append(exception.getMessage()+"\n错误的参数，位置："+this.getWidgetName()+"\n");
        }
        if (value < minNum)
        {
            value = minNum;
            ErrorReportWidget.errorReport.append("系统：超出最小值，已自动设置为理论最小值,位置："+this.getWidgetName()+"\n");
        }
        return value;
    }
}
